package com.quanshu.change.view;

import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.ShopInterface;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.LogUtil;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.shop.base.tool.BaseTool;
import com.quanshu.shop.base.view.BaseTitleBar;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoldShareActivity extends BaseActivity {

    @BindView(R.id.title)
    BaseTitleBar title;
    @BindView(R.id.txt1)
    TextView txt1;
    @BindView(R.id.ed1)
    EditText ed1;
    @BindView(R.id.cl1)
    ConstraintLayout cl1;
    @BindView(R.id.txt2)
    TextView txt2;
    @BindView(R.id.ed2)
    EditText ed2;
    @BindView(R.id.money_tips)
    TextView moneyTips;
    @BindView(R.id.cl2)
    ConstraintLayout cl2;
    @BindView(R.id.txt3)
    TextView txt3;
    @BindView(R.id.ed3)
    EditText ed3;
    @BindView(R.id.cl3)
    ConstraintLayout cl3;
    @BindView(R.id.cl)
    ConstraintLayout cl;
    @BindView(R.id.bag1)
    View bag1;
    @BindView(R.id.txt_ok)
    TextView txtOk;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gold_share);
        ButterKnife.bind(this);
        initView();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
    private void initView() {
        ImmersionBar.with(this).statusBarColor("#FFFFFF").titleBar(title).statusBarDarkFont(true).init();
        title.setLeftImage(R.drawable.icon_back_black_2_0).setCenterText("分享").setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });
        RxViewHelper.getInstance().clicks(txtOk, aVoid -> {
            //分享
            shareGold();
        });

        ed1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        ed2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s)) {
                    if (Integer.parseInt(s.toString()) < 100) {
                        moneyTips.setVisibility(View.VISIBLE);
                    } else {
                        int a = Integer.parseInt(s.toString()) % 100;
                        if (a > 0) {
                            moneyTips.setVisibility(View.VISIBLE);
                        } else {
                            moneyTips.setVisibility(View.GONE);
                        }
                    }
                } else {
                    moneyTips.setVisibility(View.VISIBLE);
                }


            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        ed3.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void shareGold() {
        if (TextUtils.isEmpty(ed1.getText().toString()) || !BaseTool.checkPhone(ed1.getText().toString())) {
            ToastHelper.toast("手机号格式不正确");
            return;
        }
        if (currentUser != null) {
            NetClient.getInstance().getShopService(ShopInterface.class)
                    .shareGoldBean(currentUser.getMemberid(), ed1.getText().toString(), ed2.getText().toString(), ed3.getText().toString())
                    .compose(new BaseTransformer<>(this, true, false))
                    .subscribe(new BaseObserver<BaseResponse<String>>() {
                        @Override
                        public void onSuccess(BaseResponse<String> response) {
                            if (response.getCode() == 1) {
                                ToastHelper.toast(response.getMsg());
                                finish();
                            }
                        }


                        @Override
                        public void onFailure(Throwable e) {
                            super.onFailure(e);
                            LogUtil.printException(e);
                        }
                    });
        }
    }


}
